package cn.iocoder.yudao.module.erp.controller.app.common;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.product.ErpProductRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.stock.vo.warehouse.ErpWarehouseRespVO;
import cn.iocoder.yudao.module.erp.controller.app.common.vo.CommonReqVO;
import cn.iocoder.yudao.module.erp.controller.app.common.vo.ErpWarehouseVO;
import cn.iocoder.yudao.module.erp.controller.app.product.vo.category.ErpAppProductCategoryList2ReqVO;
import cn.iocoder.yudao.module.erp.controller.app.product.vo.category.ErpAppProductCategoryListReqVO;
import cn.iocoder.yudao.module.erp.controller.app.product.vo.category.ErpProductCategoryRespVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductCategoryDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.stock.ErpWarehouseDO;
import cn.iocoder.yudao.module.erp.service.CommonService;
import cn.iocoder.yudao.module.erp.service.product.ErpProductService;
import cn.iocoder.yudao.module.erp.service.stock.ErpWarehouseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

@Tag(name = "应用端-ERP公用接口")
@RestController
@RequestMapping("/erp/common")
@Validated
public class CommonController {

    @Resource
    private CommonService commonService;
    @Resource
    private ErpProductService productService;
    @Resource
    private ErpWarehouseService warehouseService;

    @GetMapping("/productCategoryFirstList")
    @Operation(summary = "获得产品一级分类列表")
    public CommonResult<List<ErpProductCategoryRespVO>> productCategoryFirstList(@Valid ErpAppProductCategoryListReqVO listReqVO) {
        List<ErpProductCategoryDO> list = commonService.productCategoryFirstList(listReqVO);
        return success(BeanUtils.toBean(list, ErpProductCategoryRespVO.class));
    }

    @GetMapping("/productCategorySecondList")
    @Operation(summary = "根据一级产品分类编号获得产品二级分类列表")
    public CommonResult<List<ErpProductCategoryRespVO>> productCategorySecondList(@Valid CommonReqVO listReqVO) {
        List<ErpProductCategoryDO> list = commonService.productCategorySecondList(listReqVO);
        return success(BeanUtils.toBean(list, ErpProductCategoryRespVO.class));
    }


    @GetMapping("/warehouseFirstList")
    @Operation(summary = "获得一级仓库列表", description = "只包含被开启的一级仓库，主要用于前端的下拉选项")
    public CommonResult<List<ErpWarehouseVO>> warehouseFirstList() {
        List<ErpWarehouseDO> list = commonService.warehouseFirstList();
        return success(BeanUtils.toBean(list, ErpWarehouseVO.class));
    }

    @GetMapping("/warehouseSecondList")
    @Operation(summary = "根据一级产品分类编号获得产品二级分类列表")
    public CommonResult<List<ErpWarehouseVO>> warehouseSecondList(@Valid CommonReqVO listReqVO) {
        List<ErpWarehouseDO> list = commonService.warehouseSecondList(listReqVO);
        return success(BeanUtils.toBean(list, ErpWarehouseVO.class));
    }



    @GetMapping("/product/simple-list")
    @Operation(summary = "获得产品精简列表", description = "只包含被开启的产品，主要用于前端的下拉选项")
    public CommonResult<List<ErpProductRespVO>> getProductSimpleList() {
        List<ErpProductRespVO> list = productService.getProductVOListByStatus(CommonStatusEnum.ENABLE.getStatus());
        return success(convertList(list, product -> new ErpProductRespVO().setId(product.getId())
                .setName(product.getName()).setBarCode(product.getBarCode())
                .setCategoryId(product.getCategoryId()).setCategoryName(product.getCategoryName())
                .setUnitId(product.getUnitId()).setUnitName(product.getUnitName())
                .setPurchasePrice(product.getPurchasePrice()).setMinPrice(product.getMinPrice())));
    }


    @GetMapping("/warehouse/simple-list")
    @Operation(summary = "获得仓库精简列表", description = "只包含被开启的仓库，主要用于前端的下拉选项")
    public CommonResult<List<ErpWarehouseRespVO>> getWarehouseSimpleList() {
        List<ErpWarehouseDO> list = warehouseService.getWarehouseListByStatus(CommonStatusEnum.ENABLE.getStatus());
        return success(convertList(list, warehouse -> new ErpWarehouseRespVO().setId(warehouse.getId())
                .setName(warehouse.getName())));
    }



}
